package st.business.tepth.business.user.busnumber.workholder.repair.work.content;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.PartLocationBean;
import st.business.tepth.business.bean.PickGoodsWrapper;
import st.business.tepth.business.bean.PostDiagnoseWrapperBean;
import st.business.tepth.business.bean.RepairTaskBean;
import st.business.tepth.business.bean.RepairTaskTypeBean;

/**
 * @author zhangyu
 * @date 2019/2/13
 */
public class RepairPresenter extends RepairContract.Presenter {

    private final RepairContract.View mView;
    private final RepairContract.Model mModel;

    public RepairPresenter(RepairContract.View view) {
        mView = view;
        mModel = new RepairModel();
    }

    @Override
    void getWorkData(String token, int repairId, int vehicleSeriesId) {
        addDisposable(mModel.getWorkData(token, repairId,
                vehicleSeriesId,
                new OnDataConvertListener<RepairTaskTypeBean>() {
                    @Override
                    public void convert(RepairTaskTypeBean workBean) {
                        ArrayList<RepairTaskBean> repairTaskList = workBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }

                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void confirmDiagnoseList(String token, int repairId, int repairTaskId,
                             int vehicleSeriesId, String faultDiagnosticList) {
        addDisposable(mModel.postDiagnoseList(token, repairId, repairTaskId, vehicleSeriesId,
                faultDiagnosticList,
                new OnDataConvertListener<PostDiagnoseWrapperBean>() {
                    @Override
                    public void convert(PostDiagnoseWrapperBean workBean) {
                        if (workBean != null) {
                            mView.postDiagnoseList(workBean.getRepairDiagnosticList());
                        } else {
                            mView.showError("数据错误");
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (655 == code) {
                            mView.troubleHadDelete(msg);
                        } else {
                            mView.showError(msg);
                        }
                    }
                }));
    }

    @Override
    void confirmProgram(final String token, final int repairId,
                        int repairTaskId,
                        final int vehicleSeriesId, String taskItemList) {
        addDisposable(mModel.postProgram(token, repairId, repairTaskId, vehicleSeriesId,
                taskItemList,
                new OnDataConvertListener<PickGoodsWrapper>() {
                    @Override
                    public void convert(PickGoodsWrapper programConfirmPickGoods) {
                        mView.showPickGoods(programConfirmPickGoods);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                },
                new OnDataConvertListener<RepairTaskTypeBean>() {
                    @Override
                    public void convert(RepairTaskTypeBean workBean) {
                        ArrayList<RepairTaskBean> repairTaskList = workBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void deleteDiagnose(String token, int repairId,
                        int vehicleSeriesId,
                        int faultDiagnosticId, int faultDiagnosticIdType) {
        addDisposable(mModel.deleteDiagnose(token, repairId, vehicleSeriesId, faultDiagnosticId,
                faultDiagnosticIdType,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.deleteDiagnoseSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                },
                new OnDataConvertListener<RepairTaskTypeBean>() {
                    @Override
                    public void convert(RepairTaskTypeBean workBean) {
                        ArrayList<RepairTaskBean> repairTaskList = workBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void deleteProgram(String token, int repairId, int faultDiagnosticId,
                       int planId, final int position) {
        addDisposable(mModel.deleteProgram(token, repairId, faultDiagnosticId, planId,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String workBean) {
                        mView.deleteProgramSucceed(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void submitFinishWork(String token, int repairId, int repairTaskId, int vehicleSeriesId) {
        addDisposable(mModel.submitFinishWork(token, repairId, repairTaskId, vehicleSeriesId,
                new OnDataConvertListener<RepairTaskTypeBean>() {
                    @Override
                    public void convert(RepairTaskTypeBean workBean) {
                        ArrayList<RepairTaskBean> repairTaskList = workBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }
                        mView.finishSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }, new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.submitFinishSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void pickGoods(String token, int repairId,
                   int vehicleSeriesId, String materialApplyList) {
        addDisposable(mModel.pickGoods(token, repairId, vehicleSeriesId, materialApplyList,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.pickGoodsSucceed();
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }, new OnDataConvertListener<RepairTaskTypeBean>() {

                    @Override
                    public void convert(RepairTaskTypeBean repairTaskTypeBean) {
                        ArrayList<RepairTaskBean> repairTaskList = repairTaskTypeBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void returnGoods(String token, String items, int planId,
                     final int position) {
        addDisposable(mModel.returnGoods(token, items, planId,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.returnGoodsSucceed(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void addCoWorker(String token, int id, final int position) {
        addDisposable(mModel.addCoWorker(token, id,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.addCoWorkerSucceed(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void cancelCoWorker(String token, int id, final int position) {
        addDisposable(mModel.cancelCoWorker(token, id,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.cancelCoWorkerSucceed(position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    @Override
    void cancelFinishWork(String token, int repairId, int repairTaskId, int vehicleSeriesId) {
        addDisposable(mModel.cancelFinishWork(token, repairId, repairTaskId, vehicleSeriesId,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        if (mView != null) {
                            mView.cancelFinishWorkSucceed();
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showError(msg);
                        }
                    }
                }, new OnDataConvertListener<RepairTaskTypeBean>() {
                    @Override
                    public void convert(RepairTaskTypeBean repairTaskTypeBean) {
                        ArrayList<RepairTaskBean> repairTaskList = repairTaskTypeBean.getRepairTaskList();
                        if (repairTaskList != null && !repairTaskList.isEmpty()) {
                            mView.showRepairTask(repairTaskList.get(0));
                        } else {
                            mView.showRepairTask(null);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showError(msg);
                        }
                    }
                }));
    }

    @Override
    void addTempLocation(String token, final int position,
                         final PartLocationBean partLocationBean, String tempLocation) {
        addDisposable(mModel.addTempLocation(token, tempLocation,
                new OnDataConvertListener<List<PartLocationBean>>() {
                    @Override
                    public void convert(List<PartLocationBean> locationBeans) {
                        PartLocationBean partLocationBeanNew = partLocationBean;
                        if (partLocationBean == null) {
                            partLocationBeanNew = new PartLocationBean();
                        }
                        join(locationBeans, partLocationBeanNew);
                        mView.showTempLocation(partLocationBeanNew, position);
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                }));
    }

    private void join(List<PartLocationBean> list, PartLocationBean newP) {
        if (list != null) {
            String name = newP.getPositionName() == null ? "" : newP.getPositionName() + ",";
            String code = newP.getPositionCode() == null ? "" : newP.getPositionCode() + ",";

            for (PartLocationBean partLocationBean : list) {
                name += partLocationBean.getPositionName() + ",";
                code += partLocationBean.getPositionCode() + ",";
            }
            name = name.substring(0, name.length() - 1);
            code = code.substring(0, code.length() - 1);
            newP.setPositionName(name);
            newP.setPositionCode(code);
        }
    }
}
